
package com.core.controller;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.core.util.B5MAppManager;
import com.cptravel.R;

public abstract class B5MBaseActivity extends FragmentActivity
{
    protected String mTrackActivity = ""; // GA 统计
    public Context mContext;
    public Activity mActivity;
    public Button bt_left, bt_right;
    public TextView tv_title;
    public ProgressBar mProgressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        if (!isNoTitle())
        {
            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        }
        else
        {
            requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        doBefore(savedInstanceState);
        View containerView = (doGetContentView() == null) ? getLayoutInflater().inflate(doGetContentViewId(), null) : doGetContentView();
        setContentView(containerView);
        if (!isNoTitle())
        {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, getCustomTitleLayoutResId());
        }
        init();
        doInitSubViews(containerView);
        doInitDataes();
        doAfter();
        //
        // 添加Activity到堆栈
        B5MAppManager.getInstance().addActivity(this);
    }

    /**
     * 设置该activity是否有标题栏</br>
     * true没标题栏 Window.FEATURE_NO_TITLE</br>
     * false有标题栏 Window.FEATURE_CUSTOM_TITLE
     * @return
     */
    public boolean isNoTitle()
    {
        // TODO Auto-generated method stub
        return false;
    }

    /**
     * 自定义标题栏布局，默认是b5mei_head.xml
     * @return
     */
    protected int getCustomTitleLayoutResId()
    {
        // TODO Auto-generated method stub
        return R.layout.b5mei_head;
    }

    private void init()
    {
        // TODO Auto-generated method stub
        if (!isNoTitle())
        {
            initTitle();
        }
        mActivity = this;
        mContext = this;
    }

    private void initTitle()
    {
        bt_left = (Button) findViewById(R.id.bt_head_left);
        bt_left.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                onHeadLeftButtonClicked();
            }
        });
        bt_right = (Button) findViewById(R.id.bt_head_right);
        bt_right.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                onHeadRightButtonClicked();
            }
        });
        tv_title = (TextView) findViewById(R.id.tv_head_title);
        tv_title.setText(getHeadTitle());
        mProgressBar = (ProgressBar) findViewById(R.id.pb_head);
    }

    /**
     * 设置标题栏title
     * @return
     */
    protected abstract String getHeadTitle();

    /**
     * 标题栏左边按钮点击事件处理,默认是finish
     */
    protected void onHeadLeftButtonClicked()
    {
        // TODO Auto-generated method stub
        finish();
    }

    /**
     * 标题栏右边按钮点击事件处理
     */
    protected void onHeadRightButtonClicked()
    {
        // TODO Auto-generated method stub
    }

    public abstract int doGetContentViewId();

    public View doGetContentView()
    {
        return null;
    }

    public abstract void doInitSubViews(View containerView);

    public abstract void doInitDataes();

    public abstract void doAfter();

    protected void doBefore(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onStart()
    {
        super.onStart();
    }

    @Override
    protected void onStop()
    {
        super.onStop();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        // 结束Activity&从堆栈中移除
        B5MAppManager.getInstance().finishActivity(this);
    }

    public void sendEvent(String action , String label)
    {
    }

    /**
     * 显示头部网络加载进度圈
     */
    public void showHeadProgressBar()
    {
        mProgressBar.setVisibility(View.VISIBLE);
    }

    /**
     * 隐藏头部网络加载进度圈
     */
    public void hideHeadProgressBar()
    {
        mProgressBar.setVisibility(View.GONE);
    }
}
